#define DBG_MEM
#define _GNU_SOURCE
#include "memleak.h"
#include "urlmatch.h"
#include "list.h"

struct list_head mem_node_list = LIST_HEAD_INIT(mem_node_list);
pthread_mutex_t memleak_lock = PTHREAD_MUTEX_INITIALIZER;

static inline int urlmatch_match_test(struct urlmatch *m, urlmatch_pattern_t *urlmatch_pattern, const char *host, int host_len, const char *path, int path_len, int match_host_pos, int match_path_pos, void *context)
{
    if (m && urlmatch_pattern && host && host_len && path && path_len && context) { }
    //printf("host_pos %3d path_pos %3d match url %.*s%.*s\n", match_host_pos, match_path_pos, host_len - match_host_pos, host + match_host_pos, match_path_pos + 1,path);
    return 0;
}

struct urlmatch_userdata {
    int host_len;
    int path_len;
};

//#define HOSTNAME_HEAD "1234567890123456789012345678901234567890123456789012"
#define HOSTNAME_HEAD "12"
#define SIG1 HOSTNAME_HEAD"com.cn"
#define SIG2 HOSTNAME_HEAD".com.cn"
#define SIG3 HOSTNAME_HEAD"www.example.com.cn"
#define SIG4 HOSTNAME_HEAD"www.example.com.cn"
#define SIG5 HOSTNAME_HEAD"www.example.com.cn"
#define TEST_URL_HOST HOSTNAME_HEAD"www.example.com.cn"
#define TEST_URL_PATH "/index.html"
int main(void)
{
    struct urlmatch *m;
    struct urlmatch_userdata *userdata;

    for (;;)
    {
        m = urlmatch_new(NULL, URLMATCH_DEFAULT_CHAR_LIST, URLMATCH_DEFAULT_CHAR_LIST_LEN);

        //userdata = calloc(1, sizeof(*userdata)); userdata->host_len = 6 , userdata->path_len = -1; urlmatch_add_url_extra(m, SIG1 , strlen(SIG1), NULL, 0, userdata, 0);
        //userdata = calloc(1, sizeof(*userdata)); userdata->host_len = 7 , userdata->path_len = -1; urlmatch_add_url_extra(m, SIG2 , strlen(SIG2), NULL, 0, userdata, 0);
        //userdata = calloc(1, sizeof(*userdata)); userdata->host_len = 18, userdata->path_len = -1; urlmatch_add_url_extra(m, SIG3 , strlen(SIG3), NULL , 0, userdata, 0);
        //userdata = calloc(1, sizeof(*userdata)); userdata->host_len = 18, userdata->path_len = 1 ; urlmatch_add_url_extra(m, SIG4 , strlen(SIG4), "/"  , 1, userdata, 0);
        userdata = calloc(1, sizeof(*userdata)); userdata->host_len = 18, userdata->path_len = 3 ; urlmatch_add_url_extra(m, SIG5 , strlen(SIG5), "/in", 3, userdata, 0);
        urlmatch_stat(m);
        time_t start = time(NULL);
        int i;
        for (i = 0; i <  100000000; i++) {
            urlmatch_find(m, TEST_URL_HOST, strlen(TEST_URL_HOST), TEST_URL_PATH, strlen(TEST_URL_PATH), urlmatch_match_test, NULL);
        }
        time_t end = time(NULL);
        printf("%lu\n", end - start);
        exit(21);
        urlmatch_free(m);
        memleak_stat(NULL, 0);
        break;
    }

    return 0;
}
